/**
 * retailProfit.jsp引用
 * @param $
 * @param win
 * @param doc
 */
var url = '/business/retailProfitServlet';
var sumType = 10;
(function($, win, doc){
	// 查询条件
	var createCondCombox = function(){
		var param = Param.getReadCmd();
		var cond = {};
		param = Param.buildParam(param,cond);
		param[Param.CMDTYPEKEY] = 0;
		param[Param.CMDVALUEKEY] = 1;
		
		// 区域
		$('#area').combobox({
		    url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
		// 分店
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 1;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#shop').combobox({
			url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
		// 商品类型
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 2;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#goodsType').combobox({
			url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
		// 供应商
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 3;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#supplier').combobox({
			url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
		// 品牌
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 4;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#brand').combobox({
			url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
		// 经营方式
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 5;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#shopType').combobox({
			url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
	};
	/**
	 * 创建条件对象
	 */
	var getCond = function() {
		var cond = {};
		// 时间
		var startTime = $('#startTime').datebox('getValue');
		cond['startTime'] = Param.buildParamItem('=', startTime, Param.SQLType.VARCHAR); 
		var endTime = $('#endTime').datebox('getValue');
		cond['endTime'] = Param.buildParamItem('=', endTime, Param.SQLType.VARCHAR); 
		//区域
		var area = $('#area').combobox('getValue');
		cond['area'] = Param.buildParamItem('=', area, Param.SQLType.VARCHAR); 
		//分店
		var shop = $('#shop').combobox('getValue');
		cond['shop'] = Param.buildParamItem('=', shop, Param.SQLType.VARCHAR); 
		//商品类型
		var goodsType = $('#goodsType').combobox('getValue');
		cond['goodsType'] = Param.buildParamItem('=', goodsType, Param.SQLType.VARCHAR); 
		//品牌
		var brand = $('#brand').combobox('getValue');
		cond['brand'] = Param.buildParamItem('=', brand, Param.SQLType.VARCHAR); 
		//供应商
		var supplier = $('#supplier').combobox('getValue');
		cond['supplier'] = Param.buildParamItem('=', supplier, Param.SQLType.VARCHAR); 
		//经营方式
		var shopType = $('#shopType').combobox('getValue');
		cond['shopType'] = Param.buildParamItem('=', shopType, Param.SQLType.VARCHAR); 
		//货号
		var artNo = $('#artNo').val();
		if(artNo==""){
			artNo = "-";
		}
		cond['artNo'] = Param.buildParamItem('=', '%'+artNo+'%', Param.SQLType.VARCHAR); 
		return cond;
	};
	// 查询
	var onQuery = function(){
		var cond = getCond();
		if(!Util.nvl(cond) && cond == false){
			return ;
		}
		var param = Param.getReadCmd();
		param = Param.buildParam(param, cond);
		var dgCol;
		if(sumType==10){
			param[Param.CMDTYPEKEY] = 0;
			dgCol = [[
				    {field: '_rn',title: '行号',align: 'center',width: 30
			    	,formatter: function(value, row, index) {
			    		return index+1;
			    	}
					},
					{field: 'AREA_ID',title: '区域编码',width: 60,align:'center'},
				    {field: 'AREA_NAME',title: '区域名称',width: 160,align:'center'},
				    {field: 'SHOP_ID',title: '分店编码',width: 60,align:'center'},
				    {field: 'SHOP_NAME',title: '分店名称',width: 100,align:'center'},
				    {field: 'BAR_CODE',title: '货号',width: 120,align:'center'},
				    {field: 'PRODUCT_NAME',title: '品名',width: 100,align:'center'},
				    {field: 'UNIT',title: '单位',width: 50,align:'center'},
				    {field: 'SPEC',title: '规格',width: 60,align:'center'},
				    {field: 'SELF_CODE',title: '自编码',width: 70,align:'center'},
				    {field: 'SHOP_TYPE',title: '经营方式',width: 40,align:'center'},
				    {field: 'GROSS_ROFIT',title: '毛利',width: 60,align:'center'},
				    {field: 'QUANTITY',title: '销售数量',width: 60,align:'center'},
				    {field: 'RTL_PRICE_AMOUNT',title: '销售金额',width: 60,align:'center'},
				    {field: 'CST_PRICE_AMOUNT',title: '销售成本',width: 60,align:'center'},
				    {field: 'AMT_QUANTITY',title: '不含赠送销售数量',width: 100,align:'center'},
				    {field: 'AMT_AMOUNT',title: '不含赠送销售金额',width: 100,align:'center'},
				    {field: 'AMT_GROSS_ROFIT',title: '不含赠送的毛利',width: 100,align:'center'},
				    {field: 'RTL_PRICE',title: '参考原价',width: 60,align:'center'},
				    {field: 'RTL_PRICE_AMOUNT',title: '原价金额',width: 60,align:'center'},
				    {field: 'GROSS_ROFIT_PERCENT',title: '毛利率',width: 60,align:'center'},
				    {field: 'PRODUCT_TYPE_ID',title: '类别编码',width: 80,align:'center'},
				    {field: 'PROD_TYPE_NAME',title: '类别名称',width: 60,align:'center'},
				    {field: 'BRAND_ID',title: '品牌编码',width: 60,align:'center'},
				    {field: 'BRAND_NAME',title: '品牌名称',width: 60,align:'center'},
				    {field: 'SUPPLIER_ID',title: '供应商编码',width: 70,align:'center'},
				    {field: 'SUPPLIER_NAME',title: '供应商名称',width: 70,align:'center'}
			]];
		}else if(sumType==11){
			param[Param.CMDTYPEKEY] = 1;
			dgCol = [[
					    {field: '_rn',title: '行号',align: 'center',width: 30
					    	,formatter: function(value, row, index) {
					    		return index+1;
					    	}
						},
						{field: 'PRODUCT_TYPE_ID',title: '类别编码',width: 100,align:'center'},
					    {field: 'PROD_TYPE_NAME',title: '类别名称',width: 120,align:'center'},
					    {field: 'GROSS_ROFIT',title: '毛利',width: 80,align:'center'},
					    {field: 'RTL_PRICE_AMOUNT',title: '销售金额',width: 80,align:'center'},
					    {field: 'CST_PRICE_AMOUNT',title: '销售成本',width: 80,align:'center'},
					    {field: 'AMT_AMOUNT',title: '不含赠送销售金额',width: 120,align:'center'},
					    {field: 'AMT_GROSS_ROFIT',title: '不含赠送的毛利',width: 120,align:'center'},
					    {field: 'RTL_PRICE_AMOUNT',title: '原价金额',width: 80,align:'center'},
					    {field: 'GROSS_ROFIT_PERCENT',title: '毛利率',width: 80,align:'center'}
				]];
		}else if(sumType==12){
			param[Param.CMDTYPEKEY] =2;
			dgCol = [[
					    {field: '_rn',title: '行号',align: 'center',width: 30
					    	,formatter: function(value, row, index) {
					    		return index+1;
					    	}
						},
						{field: 'BRAND_ID',title: '品牌编码',width: 100,align:'center'},
					    {field: 'BRAND_NAME',title: '品牌名称',width: 120,align:'center'},
					    {field: 'GROSS_ROFIT',title: '毛利',width: 80,align:'center'},
					    {field: 'RTL_PRICE_AMOUNT',title: '销售金额',width: 80,align:'center'},
					    {field: 'CST_PRICE_AMOUNT',title: '销售成本',width: 80,align:'center'},
					    {field: 'AMT_AMOUNT',title: '不含赠送销售金额',width: 120,align:'center'},
					    {field: 'AMT_GROSS_ROFIT',title: '不含赠送的毛利',width: 120,align:'center'},
					    {field: 'RTL_PRICE_AMOUNT',title: '原价金额',width: 80,align:'center'},
					    {field: 'GROSS_ROFIT_PERCENT',title: '毛利率',width: 80,align:'center'}
				]];
		}else if(sumType==13){
			param[Param.CMDTYPEKEY] =3;
			dgCol = [[
					    {field: '_rn',title: '行号',align: 'center',width: 30
					    	,formatter: function(value, row, index) {
					    		return index+1;
					    	}
						},
						{field: 'SHOP_ID',title: '分店编码',width: 100,align:'center'},
					    {field: 'SHOP_NAME',title: '分店名称',width: 120,align:'center'},
					    {field: 'GROSS_ROFIT',title: '毛利',width: 80,align:'center'},
					    {field: 'RTL_PRICE_AMOUNT',title: '销售金额',width: 80,align:'center'},
					    {field: 'CST_PRICE_AMOUNT',title: '销售成本',width: 80,align:'center'},
					    {field: 'AMT_AMOUNT',title: '不含赠送销售金额',width: 120,align:'center'},
					    {field: 'AMT_GROSS_ROFIT',title: '不含赠送的毛利',width: 120,align:'center'},
					    {field: 'RTL_PRICE_AMOUNT',title: '原价金额',width: 80,align:'center'},
					    {field: 'GROSS_ROFIT_PERCENT',title: '毛利率',width: 80,align:'center'}	   
				]];
		}
		param = Param.buildParam(param, cond);
		$('#dg').datagrid({
			url: CONTEXT_PATH +url,
			queryParams: param,
			method: 'POST',
			columns:dgCol
		});
	};
	// 导出excel
	var onExpExcel = function() {
		var cond = getCond();
		if (!cond) {
			return false;
		}
		var param = Param.getExportCmd();
		param = Param.buildParam(param, cond);
		if(sumType==10){
			param[Param.CMDTYPEKEY] = 0;
		}else if(sumType==11){
			param[Param.CMDTYPEKEY] = 1;
		}else if(sumType==12){
			param[Param.CMDTYPEKEY] = 2;
		}else if(sumType==13){
			param[Param.CMDTYPEKEY] = 3;
		}else if(sumType==14){
			param[Param.CMDTYPEKEY] = 4;
		}else if(sumType==15){
			param[Param.CMDTYPEKEY] = 5;
		}
		Util.doPost(url, param, function(data) {
			if (data && data.file) {
				win.open(data.file);
			} else if (data && data.stateMsg) {
				Util.msg.alert(data.stateMsg);
				return false;
			}
		});
	};
	
	// 初始化
	$(function(){
		// dg
		$('#dg').datagrid({
			fit: true,
            onLoadSuccess: compute,//页面刷新的时候触发求和的函数
            columns: [[
			    {field: '_rn',title: '行号',align: 'center',width: 30
				    	,formatter: function(value, row, index) {
				    		return index+1;
				    	}
				},
			    {field: 'AREA_ID',title: '区域编码',width: 60,align:'center'},
			    {field: 'AREA_NAME',title: '区域名称',width: 160,align:'center'},
			    {field: 'SHOP_ID',title: '分店编码',width: 60,align:'center'},
			    {field: 'SHOP_NAME',title: '分店名称',width: 100,align:'center'},
			    {field: 'BAR_CODE',title: '货号',width: 120,align:'center'},
			    {field: 'PRODUCT_NAME',title: '品名',width: 100,align:'center'},
			    {field: 'UNIT',title: '单位',width: 50,align:'center'},
			    {field: 'SPEC',title: '规格',width: 60,align:'center'},
			    {field: 'SELF_CODE',title: '自编码',width: 70,align:'center'},
			    {field: 'SHOP_TYPE',title: '经营方式',width: 40,align:'center'},
			    {field: 'GROSS_ROFIT',title: '毛利',width: 60,align:'center'},
			    {field: 'QUANTITY',title: '销售数量',width: 60,align:'center'},
			    {field: 'RTL_PRICE_AMOUNT',title: '销售金额',width: 60,align:'center'},
			    {field: 'CST_PRICE_AMOUNT',title: '销售成本',width: 60,align:'center'},
			    {field: 'AMT_QUANTITY',title: '不含赠送销售数量',width: 100,align:'center'},
			    {field: 'AMT_AMOUNT',title: '不含赠送销售金额',width: 100,align:'center'},
			    {field: 'AMT_GROSS_ROFIT',title: '不含赠送的毛利',width: 100,align:'center'},
			    {field: 'RTL_PRICE',title: '参考原价',width: 60,align:'center'},
			    {field: 'RTL_PRICE_AMOUNT',title: '原价金额',width: 60,align:'center'},
			    {field: 'GROSS_ROFIT_PERCENT',title: '毛利率',width: 60,align:'center'},
			    {field: 'PRODUCT_TYPE_ID',title: '类别编码',width: 80,align:'center'},
			    {field: 'PROD_TYPE_NAME',title: '类别名称',width: 60,align:'center'},
			    {field: 'BRAND_ID',title: '品牌编码',width: 60,align:'center'},
			    {field: 'BRAND_NAME',title: '品牌名称',width: 60,align:'center'},
			    {field: 'SUPPLIER_ID',title: '供应商编码',width: 70,align:'center'},
			    {field: 'SUPPLIER_NAME',title: '供应商名称',width: 70,align:'center'}
			]]
		});
		//创建条件查询下拉组件
		createCondCombox();
		// 初始化时间
		var cdate = Util.date.format(new Date(), 'yyyy-MM-dd');
		$('#startTime').datebox('setValue', cdate);
		$('#endTime').datebox('setValue', cdate);
		
		$(":radio").click(function(){
			var radioValue = $(this).val();
			if(radioValue>9){
				sumType = radioValue;
			}else{
				pubFunction.setTime(radioValue);
			}
	    });
		
		// 执行查询
		$('#queryBtn').off('click').on('click', onQuery);
		// 执行导出
		$('#exportBtn').on('click', onExpExcel);
		// 执行退出
		$('#closeBtn').on('click', pubFunction.CloseWebPage);
		// 执行打印
		$('#printBtn').on('click', pubFunction.PrintPage);

	});


    function compute() {
        var rows = $("#dg").datagrid("getRows")//获取当前的数据行

		//计算总和
        var rtotal = 0, hctotal = 0
            , wftotal = 0, zgwctotal = 0, mzgtotal = 0, is_heji = 0;
        for (var i = 0; i < rows.length; i++) {
            rtotal += parseFloat(rows[i]['QUANTITY']);
            hctotal += parseFloat(rows[i]['RTL_PRICE_AMOUNT']);
            wftotal += parseFloat(rows[i]['CST_PRICE_AMOUNT']);
            zgwctotal += parseFloat(rows[i]['AMT_QUANTITY']);
            mzgtotal += parseFloat(rows[i]['AMT_AMOUNT']);


            if (rows[i]['areaname'] == "<b>合计：</b>") {
                is_heji = 1;
            }}
        if (is_heji == 0) {
//新增一行显示统计信息
            $("#dg").datagrid('appendRow', {
                AREA_ID: '<b>合计：</b>',
                QUANTITY: rtotal, RTL_PRICE_AMOUNT: hctotal,
                CST_PRICE_AMOUNT:wftotal,
                AMT_QUANTITY:zgwctotal,
                AMT_AMOUNT:mzgtotal
                // findProblemRate: b,
                // completeRectificationRate: c, zongfindProblem: wftotal, completeRectification: zgwctotal,
                // noCompleteRectification: mzgtotal, completeTaskRate: a
            });
        }
    }
})(jQuery, window, document);
